import { defineStore } from "pinia";
import { store } from "@/store";
import { ref } from "vue";
import { HMI_NPI_API } from "@/api/hmi/npi.api";
import type { NpiClearResponse, NpiClearType } from "@/api/hmi/npi.api";

/**
 * NPI Store
 */
export const useNpiStore = defineStore("npi", () => {
  // 响应式状态
  const isClearingUser = ref<boolean>(false);
  const isClearingNpi = ref<boolean>(false);
  const lastUserClearResult = ref<NpiClearResponse | null>(null);
  const lastNpiClearResult = ref<NpiClearResponse | null>(null);

  // 计算属性 - 是否有任何清零操作正在进行
  const isClearing = ref<boolean>(false);

  /**
   * 用户数据清零
   * @param cpu CPU 编号
   * @returns Promise<NpiClearResponse>
   */
  const clearUserData = (cpu: number = 1) => {
    return new Promise<NpiClearResponse>((resolve, reject) => {
      isClearingUser.value = true;
      isClearing.value = true;

      HMI_NPI_API.clearUserData(cpu)
        .then((res) => {
          lastUserClearResult.value = res;
          resolve(res);
        })
        .catch((error) => {
          console.error("clearUserData error:", error);
          reject(error);
        })
        .finally(() => {
          isClearingUser.value = false;
          isClearing.value = isClearingNpi.value;
        });
    });
  };

  /**
   * NPI 数据清零
   * @param cpu CPU 编号
   * @returns Promise<NpiClearResponse>
   */
  const clearNpiData = (cpu: number = 1) => {
    return new Promise<NpiClearResponse>((resolve, reject) => {
      isClearingNpi.value = true;
      isClearing.value = true;

      HMI_NPI_API.clearNpiData(cpu)
        .then((res) => {
          lastNpiClearResult.value = res;
          resolve(res);
        })
        .catch((error) => {
          console.error("clearNpiData error:", error);
          reject(error);
        })
        .finally(() => {
          isClearingNpi.value = false;
          isClearing.value = isClearingUser.value;
        });
    });
  };

  /**
   * 通用数据清零
   * @param cpu CPU 编号
   * @param type 清零类型
   * @returns Promise<NpiClearResponse>
   */
  const clearData = (cpu: number = 1, type: NpiClearType) => {
    return new Promise<NpiClearResponse>((resolve, reject) => {
      const isUserData = type === NpiClearType.USER_DATA;

      if (isUserData) {
        isClearingUser.value = true;
      } else {
        isClearingNpi.value = true;
      }
      isClearing.value = true;

      HMI_NPI_API.clearData(cpu, type)
        .then((res) => {
          if (isUserData) {
            lastUserClearResult.value = res;
          } else {
            lastNpiClearResult.value = res;
          }
          resolve(res);
        })
        .catch((error) => {
          console.error("clearData error:", error);
          reject(error);
        })
        .finally(() => {
          if (isUserData) {
            isClearingUser.value = false;
          } else {
            isClearingNpi.value = false;
          }
          isClearing.value = isClearingUser.value || isClearingNpi.value;
        });
    });
  };

  /**
   * 重置用户数据清零结果
   */
  const resetUserClearResult = () => {
    lastUserClearResult.value = null;
  };

  /**
   * 重置 NPI 数据清零结果
   */
  const resetNpiClearResult = () => {
    lastNpiClearResult.value = null;
  };

  /**
   * 重置所有清零结果
   */
  const resetAllClearResults = () => {
    lastUserClearResult.value = null;
    lastNpiClearResult.value = null;
  };

  return {
    // 状态
    isClearing,
    isClearingUser,
    isClearingNpi,
    lastUserClearResult,
    lastNpiClearResult,

    // Actions
    clearUserData,
    clearNpiData,
    clearData,
    resetUserClearResult,
    resetNpiClearResult,
    resetAllClearResults,
  };
});

/**
 * 适用于 setup 之外的场景
 */
export function useNpiStoreWithOut() {
  return useNpiStore(store);
}
